package cn.jinjimi.app.main.adapter.recycler;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.drawable.ScalingUtils;
import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.CardHeader;
import cn.jinjimi.app.entity.CardItemData;
import cn.jinjimi.app.interfaces.IRVGridHeaderable;
import cn.jinjimi.app.main.fragment.CardFragment;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

/**
 * Created by lforxeverc on 2016/10/18.
 */

public abstract class CardRecAdapter extends MyBaseRecyclerAdapter<CardItemData, CardRecAdapter.ViewHolder> implements IRVGridHeaderable {
    Activity mContext;
    RecyclerView mRecyclerView;
    CardFragment.LoadType mType;

    public CardRecAdapter(Activity mContext, RecyclerView mRecyclerView, CardFragment.LoadType type) {
        this.mContext = mContext;
        this.mRecyclerView = mRecyclerView;
        this.mType = type;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mType == CardFragment.LoadType.HOLIDAY && viewType == CardItemData.Type.HEADER.ordinal()) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_card_header, parent, false);
            return new HeaderViewHolder(v);
        } else if (mType == CardFragment.LoadType.SCENE_LIST) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_scene_item, parent, false);
            return new SceneViewHolder(v);
        } else {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_card_item, parent, false);
            return new ItemViewHolder(v);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).type.ordinal();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        holder.init(getItem(position), position);
    }

    @Override
    protected Animator[] getAnimators(View view) {
        return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)};
    }

    @Override
    public boolean isHeader(int position) {

        return getItemViewType(position) == CardItemData.Type.HEADER.ordinal();
    }

    abstract class ViewHolder extends MyBaseRecyclerAdapter.MyViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void init(CardItemData data, int position);
    }

    private int mHeaderViewHeight = 0;

    public int getHeaderViewHeight() {
        return mHeaderViewHeight;
    }

    private int mItemViewHeight = 0;

    public int getItemViewHeight() {
        return mItemViewHeight;
    }

    class ItemViewHolder extends ViewHolder {
        FrescoImageView ivCard;

        public ItemViewHolder(final View itemView) {
            super(itemView);
            itemView.post(new Runnable() {
                @Override
                public void run() {
                    mItemViewHeight = itemView.getHeight();
                }
            });
            ivCard = (FrescoImageView) itemView.findViewById(R.id.iv_card);
        }

        @Override
        public void init(final CardItemData data, int position) {
            ivCard.loadView(data.itemData.getCoverUrl());
            ivCard.setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            ivCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick(data);
                }
            });
        }

    }

    public abstract void onItemClick(final CardItemData data);

    class HeaderViewHolder extends ViewHolder {
        View emptyView;
        TextView tvTime, tvName, tvRestDays;

        public HeaderViewHolder(final View itemView) {
            super(itemView);
            itemView.post(new Runnable() {
                @Override
                public void run() {
                    mHeaderViewHeight = itemView.getHeight();
                }
            });
            emptyView = itemView.findViewById(R.id.v_empty0);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvRestDays = (TextView) itemView.findViewById(R.id.tv_rest_day);
        }

        @Override
        public void init(CardItemData data, int position) {
            emptyView.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
            CardHeader header = data.headerData;
            tvTime.setText(DateUtils.formatYYYYMMDD(header.startTs));
            tvName.setText(header.name);
            String restDayStr = data.headerData.restDays == 0 ? String.format("进行中") : String.format("%d天后", data.headerData.restDays);
            tvRestDays.setText(restDayStr);
        }
    }

    class SceneViewHolder extends ViewHolder {
        FrescoImageView ivCover;
        TextView tvName;

        public SceneViewHolder(View itemView) {
            super(itemView);
            ivCover = (FrescoImageView) itemView.findViewById(R.id.iv_scene);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }

        @Override
        public void init(final CardItemData data, int position) {
            ivCover.loadView(data.sceneData.coverUrl, R.drawable.default_bg);
            tvName.setText(data.sceneData.name);
            ivCover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick(data);
                }
            });
        }
    }

}
